package info.openlvb.openface.client.base.person;

import info.openlvb.openface.client.handler.AddMailAddressListener;
import info.openlvb.openface.client.handler.DeleteUserListener;
import info.openlvb.openface.client.handler.HomePathAddressListener;
import info.openlvb.openface.client.handler.ModifyAccountListener;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.FieldListenerAdapter;

public class ModifyAccountWindow extends DialogBox{

	private ListPersonPanel listPersonPanel;
	private TextField mail,pseudo,rep,nom,prenom;
	private String old_pseudo;

	
	public ModifyAccountWindow(ListPersonPanel listPersonPanel, Record record){
		super();
		setAnimationEnabled(true);
		setGlassEnabled(true);
		this.listPersonPanel = listPersonPanel;
		old_pseudo = record.getAsString("pseudo");

		FormPanel formPanel = new FormPanel();
        formPanel.setFrame(true);
        formPanel.setTitle("Modification d'un utilisateur");
        formPanel.setLabelWidth(75);

        pseudo = new TextField("Pseudo", "pseudo", 150);
        pseudo.setAllowBlank(false);
        pseudo.setDisabled(true);
        pseudo.setValue(record.getAsString("pseudo"));
        formPanel.add(pseudo);
        
        nom = new TextField("Nom", "nom", 150);
        nom.setAllowBlank(true);
        nom.addKeyPressListener(new EventCallback(){
			public void execute(EventObject e) {
				pseudo.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
			}
        });
        nom.addListener(new FieldListenerAdapter(){
        	public void onBlur(Field field){
        		pseudo.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
        	}
        });
        formPanel.add(nom);
        nom.setValue(record.getAsString("nom"));
        
        prenom = new TextField("Prenom", "prenom", 150);
        prenom.setAllowBlank(false);
        prenom.addKeyPressListener(new EventCallback(){
			public void execute(EventObject e) {
				pseudo.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
			}
        });
        prenom.addListener(new FieldListenerAdapter(){
        	public void onBlur(Field field){
        		pseudo.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
        	}
        });
        formPanel.add(prenom);
        prenom.setValue(record.getAsString("prenom"));
        
        mail = new TextField("Mail", "mail", 150);
        mail.setDisabled(true);
        mail.setAllowBlank(true);
        formPanel.add(mail);
        mail.setValue(record.getAsString("mail"));
        
        TextField groupe = new TextField("Groupe", "groupe", 150);
        groupe.setAllowBlank(false);
        groupe.setDisabled(true);
        formPanel.add(groupe);
        groupe.setValue(record.getAsString("groupe"));

        rep = new TextField("Répertoire Perso", "rep_access", 150);
        rep.setAllowBlank(true);
        rep.setDisabled(true);
        formPanel.add(rep);
        
        rep.setValue(record.getAsString("rep_access"));
        
        HorizontalPanel hp = new HorizontalPanel();
        Button crea_mail = new Button("Créer une adresse mail");
        if(record.getAsString("mail")!=null){
        	crea_mail.setText("Supprimer l'adresse mail");
        }
        crea_mail.addListener(new AddMailAddressListener(this));
    	hp.add(crea_mail);
    	Button crea_rep = new Button("Créer un répertoire perso");
        if(record.getAsString("rep_access")!=null){
        	crea_rep.setText("Supprimer le répertoire perso");
        }
        crea_rep.addListener(new HomePathAddressListener(this));
        hp.add(crea_rep);
        formPanel.add(hp);
        Button bt_enreg = new Button("Enregistrer");
        bt_enreg.addListener(new ModifyAccountListener(this));
        formPanel.addButton(bt_enreg);
        Button bt_supp = new Button("Supprimer");
        bt_supp.addListener(new DeleteUserListener(this));
        formPanel.addButton(bt_supp);
        Button bt_annul = new Button("Annuler");
        bt_annul.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
                ModifyAccountWindow.this.hide();
            }
        });
        formPanel.addButton(bt_annul);

		setTitle("Gestion d'utilisateur");
		add(formPanel);
		center();
		show();
	}
	public TextField getMail() {
		return mail;
	}
	public TextField getPseudo() {
		return pseudo;
	}
	public TextField getHome() {
		return rep;
	}
	public String getOldPseudo(){
		return old_pseudo;
	}
	public TextField getNom() {
		return nom;
	}
	public TextField getPrenom() {
		return prenom;
	}
	public ListPersonPanel getListPersonPanel(){
		return listPersonPanel;
	}
}
